package com.material.controller.supplier;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.material.common.annotation.LoginUser;
import com.material.common.base.CommonController;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.common.utils.DateUtils;
import com.material.common.utils.StringUtils;
import com.material.common.utils.bean.BeanUtil;
import com.material.request.ReservationAddRequest;
import com.material.request.ReservationQueryRequest;
import com.material.supplier.domain.SupplierCar;
import com.material.supplier.domain.SupplierInfo;
import com.material.supplier.domain.SupplierReservation;
import com.material.supplier.service.ISupplierCarService;
import com.material.supplier.service.ISupplierInfoService;
import com.material.supplier.service.ISupplierReservationService;
import com.material.supplier.vo.SupplierReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * @author ganxinge
 */
@Api(tags = "5.送料预约管理")
@RestController
@RequestMapping("/reservation")
public class SupplierReservationController extends CommonController {

    @Autowired
    private ISupplierReservationService supplierReservationService;
    @Autowired
    private ISupplierInfoService supplierInfoService;
    @Autowired
    private ISupplierCarService supplierCarService;

    @ApiOperation("预约列表")
    @GetMapping("/list")
    public ResultResponse list(@ApiIgnore @LoginUser SysUser sysUser, ReservationQueryRequest reservationQueryRequest){
        //checkSupplierUser(sysUser);
        SupplierReservation query = new SupplierReservation();
        if(StringUtils.isNotBlank(reservationQueryRequest.getCarNum())){
            SupplierCar supplierCar = supplierCarService.selectSupplierCarByCarNum(reservationQueryRequest.getCarNum(),sysUser.getBizId());
            if(supplierCar == null){
                query.setCarId(0L);
            }else{
                query.setCarId(supplierCar.getId());
            }
        }
        String userType = sysUser.getUserType();
        if(SysUser.USER_TYPE_3.equals(userType)){
            query.setSupplierId(sysUser.getBizId());
        }else{
            query.setFactoryId(sysUser.getBizId());
        }

        PageHelper.startPage(reservationQueryRequest.getPageNum(),reservationQueryRequest.getPageSize(),"id desc");
        List<SupplierReservationVo> supplierReservations = supplierReservationService.selectSupplierList(query);
        PageInfo<SupplierReservationVo> pageInfo = new PageInfo<>(supplierReservations);
        return ResultResponse.success(pageInfo);
    }

    @ApiOperation("预约上报")
    @PostMapping("/save")
    public ResultResponse save(@ApiIgnore @LoginUser SysUser sysUser, @RequestBody ReservationAddRequest reservationAddRequest){
        SupplierInfo supplierInfo = supplierInfoService.selectSupplierInfoById(sysUser.getBizId());
        SupplierReservation supplierReservation = BeanUtil.copyProperties(reservationAddRequest, SupplierReservation.class);
        supplierReservation.setUserId(sysUser.getUserId());
        supplierReservation.setFactoryId(supplierInfo.getFactoryId());
        supplierReservation.setSupplierId(supplierInfo.getId());
        supplierReservation.setReservationDate(reservationAddRequest.getReservationTime());
        supplierReservation.setReservationTime(reservationAddRequest.getReservationTime());
        supplierReservationService.insertSupplierReservation(supplierReservation);
        return ResultResponse.success();
    }

    @ApiOperation("今日预约列表")
    @GetMapping("/todayList")
    public ResultResponse todayList(@ApiIgnore @LoginUser SysUser sysUser, ReservationQueryRequest reservationQueryRequest){
        //checkSupplierUser(sysUser);
        SupplierReservation query = new SupplierReservation();
        if(StringUtils.isNotBlank(reservationQueryRequest.getCarNum())){
            SupplierCar supplierCar = supplierCarService.selectSupplierCarByCarNum(reservationQueryRequest.getCarNum(),sysUser.getBizId());
            if(supplierCar == null){
                query.setCarId(0L);
            }else{
                query.setCarId(supplierCar.getId());
            }
        }
        String userType = sysUser.getUserType();
        if(SysUser.USER_TYPE_3.equals(userType)){
            query.setSupplierId(sysUser.getBizId());
        }else{
            query.setFactoryId(sysUser.getBizId());
        }
        Date date = DateUtils.parseDate(DateUtils.getDate());
        query.setReservationDate(date);
        PageHelper.startPage(reservationQueryRequest.getPageNum(),reservationQueryRequest.getPageSize());
        List<SupplierReservationVo> supplierReservations = supplierReservationService.selectSupplierList(query);
        PageInfo<SupplierReservationVo> pageInfo = new PageInfo<>(supplierReservations);
        return ResultResponse.success(pageInfo);
    }
}
